package demoxsgl_300.com.gouwu1500.zhi;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Dimension;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import demoxsgl_300.com.gouwu1500.R;
import demoxsgl_300.com.gouwu1500.http.API;
import demoxsgl_300.com.gouwu1500.http.Okhttp;


public class RegistActivity extends Activity {

    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_mm)
    EditText loginMm;
    @BindView(R.id.login_queren)
    EditText loginQueren;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.textView3)
    TextView textView3;
    @BindView(R.id.login_mmd)
    Button loginMmd;
//    @BindView(R.id.tv_login)
//    TextView butLogin;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        ButterKnife.bind(this);
        setEditTextHintWithSize(loginPhone, "请输入手机号", 12);
        setEditTextHintWithSize(loginMm, "请输入密码", 12);
        setEditTextHintWithSize(loginQueren, "再次输入密码", 12);

    }

    /**
     * 设置EditText的hint字体大小
     *
     * @param editText EditText控件
     * @param hintText hint内容
     * @param size     hint字体大小，单位为sp
     */
    public static void setEditTextHintWithSize(EditText editText, String hintText, @Dimension int size) {
        if (!TextUtils.isEmpty(hintText)) {
            SpannableString ss = new SpannableString(hintText);
            //设置字体大小 true表示单位是sp
            AbsoluteSizeSpan ass = new AbsoluteSizeSpan(size, true);
            ss.setSpan(ass, 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            editText.setHint(new SpannedString(ss));
        }
    }

    @OnClick({R.id.textView2, R.id.textView3, R.id.login_mmd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textView2:
                finish();
                break;
            case R.id.textView3:
                break;
            case R.id.login_mmd:
                zhuce();
                break;
        }
    }
//    @OnClick({R.id.tv_login  })
//    public void onViewClicked(View view) {
//        switch (view.getId()) {
//
//            case R.id.tv_login:
//
//
////                API.logins(loginPhone.getText().toString(), loginMm.getText().toString(), new Okhttp.Objectcallback() {
////                    @Override
////                    public void onsuccess(String st) {
////                        Log.e("99", "登录" + st);
////                        Login register = JSONUtils.parseJSON(st, Login.class);
////                        if (register.getCode() == 10001) {
////                            if (register.isIssuccess()) {
////                                Toast.makeText(LoginActivity.this, register.getMsg(), Toast.LENGTH_SHORT).show();
////                                startActivity(new Intent(LoginActivity.this, PlaylistActivity.class));
////                                SpfUtils.put(LoginActivity.this, "islogin", true);
////                                SpfUtils.put(LoginActivity.this, "name", loginPhone.getText().toString());
////                                SpfUtils.put(LoginActivity.this, "user_uid", register.getData().getUser_uid());
////                                SpfUtils.put(LoginActivity.this, "user_name", register.getData().getUser_name());
//////                                SpfUtils.put(LoginActivity.this, "type", register.getData().getType());
////                                finish();
////                            } else {
////                                Toast.makeText(LoginActivity.this, register.getMsg(), Toast.LENGTH_SHORT).show();
////                            }
////                        } else {
////
////                        }
////
////                    }
////
////                    @Override
////                    public void onFalia(int code, String errst) {
////                        Toast.makeText(LoginActivity.this, "登录网络错误！" + errst.toString(), Toast.LENGTH_SHORT).show();
////                        Log.e("99", "登录" + errst.toString());
////                    }
////                });
//
//
//                break;
//
//
////            case R.id.but_IP:
////                startActivity(new Intent(this, SetIp_Activity.class));
////                break;
////            case R.id.but_zhu:
////                startActivity(new Intent(this, RegistActivity.class));
////                break;
////            case R.id.but_xiugai:
////                startActivity(new Intent(this, XiuGaiActivity.class));
////                break;
//
//
//        }
//    }


    public void zhuce() {
        if (TextUtils.isEmpty(loginPhone.getText().toString())) {
            Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(loginMm.getText().toString())) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (TextUtils.isEmpty(loginQueren.getText().toString())) {
            Toast.makeText(this, "请再次输入密码", Toast.LENGTH_SHORT).show();
            return;
        }
        if (!loginMm.getText().toString().equals(loginQueren.getText().toString())) {
            Toast.makeText(this, "两次输入密码不相同", Toast.LENGTH_SHORT).show();
            return;
        }
        API.Registuser(loginPhone.getText().toString(), loginMm.getText().toString(), new Okhttp.Objectcallback()

        {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx:" + st);
                if (!st.equals("注册成功")) {
                    Toast.makeText(RegistActivity.this, "注册失败," + st, Toast.LENGTH_SHORT).show();
                } else {
                    finish();
                    Toast.makeText(RegistActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                }


            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("111", "xx:" + errst);
                Toast.makeText(RegistActivity.this, "网络错误:" + errst, Toast.LENGTH_SHORT).show();
            }
        });
    }
}
